The Contribution Technique counts how many new valid subarrays are "finished" by the current element nums[i].
If the arithmetic property holds, each new element adds prev_contribution + 1 slices.
def numberOfArithmeticSlices(nums):
total, current_contrib = 0, 0
for i in range(2, len(nums)):
if nums[i] - nums[i-1] == nums[i-1] - nums[i-2]:
current_contrib += 1
total += current_contrib
else: current_contrib = 0
return total
int numberOfArithmeticSlices(vector<int>& nums) {
int total = 0, current_contrib = 0;
for (int i = 2; i < nums.size(); i++) {
if (nums[i] - nums[i-1] == nums[i-1] - nums[i-2]) {
current_contrib++;
total += current_contrib;
} else current_contrib = 0;
}
return total;
}